package com.bjpowernode.javase.exception;

public class ExceptionTest18 {
    public static void main(String[] args) {
        //System.out.println("循环内try。。catch:"+innerForeach());
       //System.out.println("循环外try。。catch:"+outForeach());
        System.out.println("循环内try。。catch,catch中抛出:"+innerForeach1());
        //System.out.println("没有try。。catch:"+noTryCatch());

    }

    public static int innerForeach(){
        int count = 0;
        for (int i = 0; i <= 10; i++) {
            try {
                if (i==3){
                    throw new RuntimeException("for循环内部try...catch");
                }
                count++;
            }catch (RuntimeException e){
                e.printStackTrace();
            }
        }
        System.out.println("这里可以被执行到。");
        return count;
    }

    public static int innerForeach1(){
        int count = 0;
        for (int i = 0; i < 10; i++) {
            try {
                if (i==3){
                    throw new RuntimeException("for循环内部try...catch");
                }
                count++;
            }catch (RuntimeException e){
                e.printStackTrace();
                throw new RuntimeException("捕获到了异常。");
            }
        }
        System.out.println("这里可以被执行到。");
        return count;
    }


    public static int outForeach(){
        int count = 0;
        try {
            for (int i = 0; i < 10; i++) {
                if (i==3){
                    throw new RuntimeException("循环外抛出异常");
                }
                count++;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        System.out.println("这里可以被执行到;");
        return count;
    }

    public static int noTryCatch(){
        int count =  0;
        for (int i = 0; i < 10; i++) {
            if (3==i){
                throw new RuntimeException("不使用try...catch.");
            }
            count ++;
        }
        return count;
    }
}
